<doctype html>
<html>
  <head>
		<script language='javascript' src='../src/jsLinq-min.js'> </script>
        
		<script language='javascript' src='json2.js'> </script>
		<script language='javascript' src='jquery.js'> </script>
		<script language='javascript'>
					
			function log(msg, iscallback) {
			    if (!iscallback) {
			        window.result = msg;
			        setTimeout("log(false, true);", 100);
			    }
			    else {
			        $("#result-field").val((typeof window.result == 'object') ? JSON.stringify(window.result) : window.result);
			        if (typeof console != "undefined")
			            console.log(window.result);
			    }
			}

			var JSLINQTester = {
			    Aggregate: function () {
			        var sum = lnq.aggregate(
                                    function (v, accumulator) {
                                        return accumulator ? v.stock + accumulator : v.stock;
                                    },
                                    1
                               );
			        log(sum);
			    },

			    All: function () {
			        var matches = lnq.all(function (v, k) { return v.stock < 10; });
			        log(matches);
			    },

			    Any: function () {
			        var matches = lnq.any(function (v, k) { return v.stock < 5; });
			        log(matches);
			    },

			    Average: function () {
			        var avg = lnq.average(function (v) { return v.stock; });
			        log(avg);
			    },


			    Concat: function () {
			        var result = lnq.concat(["Test"]);
			        log(result);
			    },

			    Contains: function () {
			        var result = lnq.contains(lnq.elementAt(10));
			        log(result);
			    },

			    Count: function () {
			        var result = lnq.count(function (v, k) { return v.name.indexOf("Item #1") != -1 ? true : false; });
			        log(result);
			    },

			    Distinct: function () {
			        var result = lnq.distinct();
			        log(result);
			    },

			    ElementAt: function () {
			        var result = lnq.elementAt(86);
			        log(result);
			    },

			    GroupBy: function () {
			        var result = lnq.groupBy(function (v) { return (v.id % 2) ? 'Odd' : 'Even' });
			        log(result);
			    },

			    IsEmpty: function () {
			        var result = lnq.isEmpty();
			        log(result);
			    },

			    Except: function () {
			        var result = lnq.except([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30], function (v, k) { return true; });
			        log(result);
			    },

			    Select: function () {
			        var result = lnq.select(
                                                function (v) { return v.stock ? (v.stock > 100 ? true : false) : false; },
                                                function (v) { return v.vendors; }
                                           );
			        log(result);
			    },
				
				OrderBy: function()
				{
					var result = lnq.orderBy(function(v) { return v.stock; }).select(null, function(v) { return v.stock; });
			        log(result);
				},
				
				OrderByDescending: function()
				{
					var result = lnq.orderByDescending(function(v) { return v.stock; }).select(null, function(v) { return v.stock; });
			        log(result);
				},
				
			    Custom: function () {
			        var _data = [];
			        for (var i = 0; i < 100000; i++)
			            _data.push(i);
			        var lnQ = Q(_data);

			        var result = lnQ.select(function (v, k) { return (v % 19999 == 0); });

			        log(result);
			    }
			};

		function TestMe() {
			    var start = new Date();
               try {
                    var inPut = $("#input-field").val();
					if(!inPut)
					throw {message:"Please select method to execute."};
						
                    var func = "function _dynamic_func() {" + inPut + "}";
                         
                    eval(func);
                    _dynamic_func();
			    }
			    catch (e) 
                {
			         $("#result-field").val(e.stack || e.message);
			     }
			     $("#info").html( "Query Completed in " + ((new Date()).getTime() - start.getTime()) + " ms" );
			}

			function InitializeSamples() {

			    var samplesize = parseInt($("#samplesize").val());
			    if (isNaN(samplesize)) {
			        $("#samplesize").val('10000');
			        samplesize = 10000;
			    }

			    data = [];
			    for (var i = 0; i < samplesize; i++)
			    {
					var element = { 
									id: i,
									name: 'Item #' + i,
									receivedOn: new Date(2014, 1, (i % 30) + 1),
									stock: parseInt(Math.random() * 15),
									vendors: []
								  };
					for(var j =0; j < element.stock; j++)
					{
						element.vendors.push({id: i * j, name: 'Vendor #' + (i * j), address: "NA", email: 'email' + (i * j) + "@domain.com", contact: [{type:1, value:'+914567890456'}]});
					}
					data.push(element);
				
					//data.push(i);
				}
				
			    lnq = Q(data);
			    $("#sampledata").val(" Array of " + typeof data[0] + "(s) 0 to " + data.length + "\n Object #1: " + JSON.stringify(data[0]));
			    $("#info").html("Total Records: " + $("#samplesize").val());
			}

			$(document).ready(function (e) {

			    InitializeSamples();
			    $("#testcase").append($("<option value=''>-- Select Case --</option>"));
			    Q.from(JSLINQTester).select(
                                            function (value) { return typeof JSLINQTester[value] == 'function' },
                                            function (value, key) {
                                                $("#testcase").append($("<option value='" + value + "'>" + value + "</option>"));
                                                return null;
                                            }
                                            );

			    $("#testcase").change(function (e) {

			        $("#input-field").val(JSLINQTester[$("#testcase").val()].toString().replace("function ()", ""));
			    });

			    $("#samplesize").change(function (e) {
			        InitializeSamples();
			    });
			});
		</script>
	</head>
	
	<body>
        <label for="testcase">API</label><select id="testcase"></select> <label for="samplesize">Total Records</label><input id="samplesize" type="text" value="10000" /> <span id="info"></span>
        <br />
		<textarea id="input-field" cols=100 rows=18 ></textarea> <textarea id="sampledata" readonly:"readonly" cols=58 rows=18 ></textarea>
        <br />
        <input type="button" onclick="TestMe()" value="Run" />
        <br />
        <textarea id="result-field" readonly:"readonly" cols=50 rows=18 style="width:100%"></textarea>
	</body>
</html>
